//
//  MinorIndexDataFactory.cpp
//  Chart
//
//  Created by CaoJun on 13-6-13.
//
//

#include "MinorIndexDataFactory.h"
#include "VOLUME.h"
#include "KDJ.h"
#include "MACD.h"
#include "RSI.h"
#include "BIAS.h"
#include "YTXChartCommon.h"
#include "YTXChartTools.h"
#include <cmath>
#include <functional>

using namespace ytx;
using namespace std;

#define YTX_GET_MINOR_INDEX(__index__, __indexConfig__) \
__index__->clearIfParentDataChanged(data);\
if(!__index__->hasData() || __index__->isChange(data, __indexConfig__)){ \
__index__->para = __indexConfig__;\
__index__->calculate(data); \
} \
else if(update){ \
    __index__->updateLastest(data); \
} \
__index__->initInfo(data); \
return __index__; 

MinorIndexDataFactory::MinorIndexDataFactory()
{
    
}

void MinorIndexDataFactory::factory(const IndexConfig indexConfig){
    switch (indexConfig.indexType) {
        case IndexType::EconomicCalendar:
            SAFEDELETE(economicCalendar);
            economicCalendar = new EconomicCalendar(indexConfig);
            break;
        case IndexType::VOLUME:
            SAFEDELETE(volume);
            volume = new VOLUME(indexConfig);
            break;
        case IndexType::Tuov:
            SAFEDELETE(tuov);
            tuov = new Tuov(indexConfig);
            break;
        case IndexType::MACD:
            SAFEDELETE(macd);
            macd = new MACD(indexConfig);
            break;
        case IndexType::KDJ:
            SAFEDELETE(kdj);
            kdj = new KDJ(indexConfig);
            break;
        case IndexType::RSI:
            SAFEDELETE(rsi);
            rsi = new RSI(indexConfig);
            break;
        case IndexType::BIAS:
            SAFEDELETE(bias);
            bias = new BIAS(indexConfig);
            break;
        //ADDIndex:
        case IndexType::CCI:
            SAFEDELETE(cci);
            cci = new CCI(indexConfig);
            break;
        default:
            // do nothing
            break;
    }
    
}

IndexData * MinorIndexDataFactory::getIndexData(IndexConfig indexConfig, bool update)
{
    IndexData * indexData = NULL;
    switch (indexConfig.indexType) {
        case IndexType::EconomicCalendar:
            indexData = getEconomicCalendar();
            break;
        case IndexType::VOLUME:
            indexData = getVOLUME(indexConfig, update);
            break;            
        case IndexType::Tuov:
            indexData = getTuov(indexConfig, update);
            break;
        case IndexType::MACD:
            indexData = getMACD(indexConfig, update);
            break;
        case IndexType::KDJ:
            indexData = getKDJ(indexConfig, update);
            break;
        case IndexType::RSI:
            indexData = getRSI(indexConfig, update);
            break;
        case IndexType::BIAS:
            indexData = getBIAS(indexConfig, update);
            break;
        case IndexType::CCI:
            indexData = getCCI(indexConfig, update);
            break;
        //ADDIndex:
        default:
            
            break;
    }
    return indexData;
}

IndexData * MinorIndexDataFactory::getEconomicCalendar(){
//    if(!economicCalendar->hasData()){
    economicCalendar->calculate(data);
//    }
    economicCalendar->initInfo(data);
    return economicCalendar;
}

IndexData * MinorIndexDataFactory::getVOLUME(IndexConfig indexConfig, bool update){
    YTX_GET_MINOR_INDEX(volume, indexConfig)
}

IndexData * MinorIndexDataFactory::getTuov(IndexConfig indexConfig, bool update){
    YTX_GET_MINOR_INDEX(tuov, indexConfig)
}

IndexData * MinorIndexDataFactory::getVOLUME(bool update){
    YTX_GET_MINOR_INDEX(volume, volume->para)
}

IndexData* MinorIndexDataFactory::getMACD(IndexConfig indexConfig, bool update)
{
    YTX_GET_MINOR_INDEX(macd, indexConfig)
}

IndexData* MinorIndexDataFactory::getKDJ(IndexConfig indexConfig, bool update)
{
    YTX_GET_MINOR_INDEX(kdj, indexConfig)
}

IndexData* MinorIndexDataFactory::getRSI(IndexConfig indexConfig, bool update)
{
    YTX_GET_MINOR_INDEX(rsi, indexConfig)
}

IndexData * MinorIndexDataFactory::getBIAS(IndexConfig indexConfig, bool update)
{
    YTX_GET_MINOR_INDEX(bias, indexConfig)
}

//ADDIndex:
IndexData * MinorIndexDataFactory::getCCI(IndexConfig indexConfig, bool update)
{
    YTX_GET_MINOR_INDEX(cci, indexConfig)
}
